package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
)

func main() {
	//http://127.0.0.1:8000/add?a=1&b=2
	//1.callID:存在于r.URL.Path 2.数据传输 url参数传输 3.网络传输本质上就是HTTP
	http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
		err := r.ParseForm()
		if err != nil {
			panic(err)
		}

		fmt.Println("path:", r.URL.Path)
		a, _ := strconv.Atoi(r.Form["a"][0])
		b, _ := strconv.Atoi(r.Form["b"][0])

		w.Header().Set("Content-Type", "application/json")
		marshal, _ := json.Marshal(map[string]int{
			"data": a + b,
		})

		w.Write(marshal)
	})

	http.ListenAndServe(":8000", nil)
}
